<%@ page language="java" import="com.alibaba.fastjson.JSONArray" pageEncoding="UTF-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="okhttp3.*" %>
<%@ page import="org.apache.axis.encoding.Base64" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.zhlx.duss.utils.PropUtil" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.UUID" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%--建模同步数据调用成本中心接口--%>
<%
    BaseBean baseBean = new BaseBean();
    String BUKRS = Util.null2String(request.getParameter("BUKRS"));
    String KOSTL = Util.null2String(request.getParameter("KOSTL"));
    Map<String, Object> apidatas = new HashMap<String, Object>();
    String code = "SUCCESS";
    JSONArray data = new JSONArray();
    String message = "";
    try {

            JSONObject dataJson = new JSONObject();
            dataJson.put("BUKRS", BUKRS);//公司代码
            dataJson.put("KOSTL", KOSTL);//成本中心
            JSONObject resultJson = handleOKHttpRequest("CO001", dataJson);
            baseBean.writeLog(">>>>>getCbzx>>>resultJson=" + resultJson);
            String status = Util.null2String(resultJson.getString("code"));
            if ("success".equals(status)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String msgText = outputJson.getString("MSG_TEXT");
                if ("S".equals(msgType) && "调用成功".equals(msgText)) {
                    String dataReturnStr = outputJson.getString("DATA");
                    JSONObject dataReturnJson = JSONObject.parseObject(dataReturnStr);
                    String returnData = dataReturnJson.getString("RETURN");
                    data = JSONArray.parseArray(returnData);
                }
            }
    } catch (Exception e) {
        e.printStackTrace();
        code = "SYSTEM_INNER_ERROR";
        message = "程序内部异常";
    }
    apidatas.put("code", code);
    apidatas.put("data", data);
    apidatas.put("message", message);
    out.print(JSONObject.toJSONString(apidatas));

%>
<%!
    private static final JSONObject propVal = PropUtil.getPropVal("zhlxsap");
    private static final String URL = propVal.getString("url")+"/RESTAdapter/ec/interface";
    private static final String USERNAME = propVal.getString("username");
    private static final String PASSWORD = propVal.getString("password");

    public static JSONObject handleOKHttpRequest(String type, JSONObject dataJson) {
        JSONObject paramsJson = new JSONObject();
        JSONObject inuptJson = new JSONObject();
        String fullDate = DateUtil.getFullDate();
        String curdate = fullDate.substring(0, 10).replace("-", "");
        String curtime = fullDate.substring(11, 19).replace(":", "");
        JSONObject headerJson = new JSONObject();
        headerJson.put("SYSID", "EC");
        headerJson.put("BUSINESS", type);
        headerJson.put("BUSNKEY", "");
        headerJson.put("TECHKEY",getUUIDStr());
        headerJson.put("DATE", curdate);
        headerJson.put("TIME", curtime);
        inuptJson.put("HEADER", headerJson);
        inuptJson.put("DATA", dataJson);
        paramsJson.put("INPUT", inuptJson.toJSONString());
        return sendOKHttpRequest(paramsJson);
    }

    public static JSONObject sendOKHttpRequest(JSONObject params) {
        JSONObject resultJson = new JSONObject();
        String code = "fail";
        String message = "";
        try {
            String authorization = " Basic " + Base64.encode((USERNAME + ":" + PASSWORD).getBytes());
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params.toJSONString());
            Request request = new Request.Builder()
                    .url(URL)
                    .method("POST", body)
                    .addHeader("Authorization", authorization)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                resultJson = JSONObject.parseObject(result);
                code = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("code", code);
        resultJson.put("message", message);
        return resultJson;
    }

    public static String getUUIDStr() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replace("-", "");
    }

%>



